home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / sbin / make-ssl-cert < prev    next >
Encoding:
Text File  |  2010-12-12  |  3.3 KB  |  118 lines

  1. #!/bin/bash -e
  2. # This is a mockup of a script to produce a snakeoil cert
  3. # The aim is to have a debconfisable ssl-certificate script
  4.  
  5. . /usr/share/debconf/confmodule
  6. db_version 2.0
  7. db_capb backup
  8.  
  9. ask_via_debconf() {
  10.     RET=""
  11.     if db_settitle make-ssl-cert/title ; then
  12.     : # OK
  13.     else
  14.     echo Debconf failed with error code $? $RET >&2
  15.     echo Maybe your debconf database is corrupt. >&2
  16.     echo Try re-installing ssl-cert. >&2
  17.     fi
  18.  
  19.     RET=""
  20.     while [ "x$RET" = "x" ]; do
  21.     db_fset make-ssl-cert/hostname seen false
  22.     db_input high make-ssl-cert/hostname || true
  23.     db_go
  24.     db_get make-ssl-cert/hostname
  25.     done
  26.     
  27.     db_get make-ssl-cert/hostname
  28.     HostName="$RET"
  29.     db_fset make-ssl-cert/hostname seen false
  30. }
  31.  
  32. make_snakeoil() {
  33.     if ! HostName="$(hostname -f)" ; then
  34.         HostName="$(hostname)"
  35.         echo make-ssl-cert: Could not get FQDN, using \"$HostName\".
  36.         echo make-ssl-cert: You may want to fix your /etc/hosts and/or DNS setup and run
  37.         echo make-ssl-cert: 'make-ssl-cert generate-default-snakeoil --force-overwrite'
  38.         echo make-ssl-cert: again.
  39.     fi
  40. }
  41.  
  42. create_temporary_cnf() {
  43.     sed -e s#@HostName@#"$HostName"# $template > $TMPFILE
  44. }
  45.  
  46. # Takes two arguments, the base layout and the output cert.
  47.  
  48. if [ $# -lt 2 ] && [ "$1" != "generate-default-snakeoil" ]; then
  49.     printf "Usage: $0 template output [--force-overwrite]\n";
  50.     printf "Usage: $0 generate-default-snakeoil [--force-overwrite]\n";
  51.     exit 1;
  52. fi
  53.  
  54. if [ "$1" != "generate-default-snakeoil" ]; then
  55.     template="$1"
  56.     output="$2"
  57.     # be anal in manual mode.
  58.     if [ ! -f $template ]; then
  59.     printf "Could not open template file: $template!\n";
  60.     exit 1;
  61.     fi
  62.     if [ -f $output ] && [ "$3" != "--force-overwrite" ]; then
  63.         printf "$output file already exists!\n";
  64.         exit 1;
  65.     fi
  66.     ask_via_debconf
  67. else
  68.     template="/usr/share/ssl-cert/ssleay.cnf"
  69.     if [ -f "/etc/ssl/certs/ssl-cert-snakeoil.pem" ] && [ -f "/etc/ssl/private/ssl-cert-snakeoil.key" ]; then
  70.         if [ "$2" != "--force-overwrite" ]; then
  71.              exit 0
  72.         fi
  73.     fi
  74.     make_snakeoil
  75. fi
  76.  
  77. # # should be a less common char
  78. # problem is that openssl virtually accepts everything and we need to
  79. # sacrifice one char.
  80.  
  81. TMPFILE="$(mktemp)" || exit 1
  82. TMPOUT="$(mktemp)"  || exit 1
  83.  
  84. trap "rm -f $TMPFILE $TMPOUT" EXIT
  85.  
  86. create_temporary_cnf
  87.  
  88. # create the certificate.
  89.  
  90. if [ "$1" != "generate-default-snakeoil" ]; then
  91.     if ! openssl req -config $TMPFILE -new -x509 -days 3650 -nodes \
  92.     -out $output -keyout $output > $TMPOUT 2>&1
  93.     then
  94.     echo Could not create certificate. Openssl output was: >&2
  95.     cat $TMPOUT >&2
  96.     exit 1
  97.     fi
  98.     chmod 600 $output
  99.     # hash symlink
  100.     cd $(dirname $output)
  101.     ln -sf $(basename $output) $(openssl x509 -hash -noout -in $(basename $output))
  102. else
  103.     if ! openssl req -config $TMPFILE -new -x509 -days 3650 -nodes \
  104.     -out /etc/ssl/certs/ssl-cert-snakeoil.pem \
  105.         -keyout /etc/ssl/private/ssl-cert-snakeoil.key > $TMPOUT 2>&1
  106.     then
  107.     echo Could not create certificate. Openssl output was: >&2
  108.     cat $TMPOUT >&2
  109.     exit 1
  110.     fi
  111.     chmod 644 /etc/ssl/certs/ssl-cert-snakeoil.pem
  112.     chmod 640 /etc/ssl/private/ssl-cert-snakeoil.key
  113.     chown root:ssl-cert /etc/ssl/private/ssl-cert-snakeoil.key
  114.     # hash symlink
  115.     cd /etc/ssl/certs/
  116.     ln -sf ssl-cert-snakeoil.pem $(openssl x509 -hash -noout -in ssl-cert-snakeoil.pem)
  117. fi
  118.